<?php

namespace app\model\system;

use app\model\BaseModel;

/**
 *
 */
class SysRole extends BaseModel
{
   
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = "sys_role";

    /**
     * The attributes that are mass assignable.
     * @var array
     */
    public function users()
    {
        return $this->belongsToMany(SysUser::class, 'sys_user_role', 'user_id', 'role_id');
    }

    /**
     * The attributes that are mass assignable.
     * @var array
     */
    public function menus()
    {
        return $this->belongsToMany(SysMenu::class, 'sys_role_menu', 'menu_id', 'role_id');
    }

    public function getMenuIds(): array
    {
        $ids = [];
        foreach ($this->menus as $menu) {
            $ids[] = $menu->id;
        }
        return $ids;
    }

    public function getFormArray()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'code' => $this->code,
            'status' => $this->status,
            'sort' => $this->sort,
            "dataScope" => $this->dataScope
        ];
    }

    public function toArray(array $merges = []): array
    {
        $modelArray = $this->getFormArray() + [
            'createTime' => $this->createTime,
            'updateTime' => $this->updateTime
        ];
        return $this->merge($modelArray, $merges);
    }
}
